package com.wistech.search.solr4.cloud;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.junit.BeforeClass;
import org.junit.Test;

import com.wistech.search.solr4.ServerFactoryImpl;
import com.wistech.search.solr4.SolrHelper;

public class CloudProductSearch {
	private static SolrServer solrServer;
	private static String host = "10.36.64.107:9983";

	@BeforeClass
	public static void beforeClass() {
		ServerFactoryImpl factory = new ServerFactoryImpl();
		factory.setServerUrl(host);
		factory.setClouded(true);
		factory.buildServer();

		solrServer = factory.getSolrServer();
		((CloudSolrServer) solrServer).setDefaultCollection("collection1");
	}

	@Test
	public void count() {
		System.out.println("总文档数: " + new SolrHelper(solrServer).count());
	}

	@Test
	public void search() {

		try {
			String keyword = "name:深圳";
			SolrQuery query = new SolrQuery();
			query.setQuery(keyword);
			QueryResponse response = solrServer.query(query);
			SolrDocumentList docs = response.getResults();

			System.out.println("totalHits：" + docs.getNumFound());
			System.out.println("搜索耗时： " + response.getQTime() + " 毫秒");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
